<template>
    <div class="container">
        <div class="title">
            <span>图文回复信息</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="100px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="图片">
                <upload-imgs
                        ref="uploadEle"
                        :value="imgValue"
                        :rules="rules"
                        :multiple="true"
                        :min-num="1"
                        :max-num="1"
                />
            </el-form-item>
            <el-form-item label="标题" prop="title">
                <el-input size="medium" v-model="form.title" placeholder="请填写标题"></el-input>
            </el-form-item>
            <el-form-item label="连接地址" prop="url">
                <el-input size="medium" v-model="form.url" placeholder="请填写连接地址"></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="summary">
                <el-input
                        size="medium"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 8}"
                        placeholder="请输入简介"
                        v-model="form.summary">
                </el-input>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm('form')">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import reply from '@/models/reply'
import UploadImgs from '@/components/base/upload-imgs'

export default {
  props: {
    replyId: {
      type: Number,
    },
  },
  name: 'ImgsUploadStage1',
  components: {
    UploadImgs,
  },
  data() {
    return {
      imgValue: [],
      rules: {
        maxSize: 0.2,
      },
      form: {
        picurl: '',
        title: '',
        url: '',
        summary: '',
        img_id: '',
      },
    }
  },
  created() {
    this.getPicInfo()
  },
  methods: {
    // 获取新闻详情
    async getPicInfo() {
      const info = await reply.getPic(this.replyId)
      if (info) {
        this.imgValue = [{
          delete: false,
          id: info.id,
          imgId: info.img_id,
          display: info.img.url,
        }]
        this.form = {
          picurl: info.img.url,
          title: info.title,
          url: info.url,
          summary: info.summary,
          img_id: info.img_id,
        }
      }
    },
    async submitForm() {
      const self = this
      const imgValue = await self.$refs.uploadEle.getValue()
      console.log(imgValue)
      const info = {
        code: self.replyId,
        img_id: imgValue[0].imgId,
        title: self.form.title,
        url: self.form.url,
        summary: self.form.summary,
        picurl: self.form.picurl,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await reply.editPic(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('picClose')
    },
    change(val) {
      this.form.content = val
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
